package org.example.config;

import org.eclipse.paho.client.mqttv3.MqttClient;
import org.eclipse.paho.client.mqttv3.MqttConnectOptions;
import org.eclipse.paho.client.mqttv3.MqttException;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;

import static org.example.util.JwtUtil.generateToken;

@Configuration
public class MqttConfig {
    @Bean
    public MqttClient mqttClient() throws MqttException {
        String brokerUrl = "tcp://yuanying.top:1883"; // EMQX服务器地址
        String clientId = "SpringBootClient";
        MqttClient mqttClient = new MqttClient(brokerUrl, clientId);

        MqttConnectOptions options = new MqttConnectOptions();
        options.setAutomaticReconnect(true);
        options.setCleanSession(true);
        options.setConnectionTimeout(10);
        String string = generateToken(null);
        options.setUserName("yourUsername"); // 设置用户名，如果需要的话
        options.setPassword(string.toCharArray());
        mqttClient.connect(options);
        return mqttClient;
    }
}
